//
//  SingleSelected_UIButton.m
//  MTSIPAD
//
//  Created by 3g2win 3g2win on 12-9-12.
//  Copyright (c) 2012年 3g2win. All rights reserved.
//

#import "SingleSelected_UIButton.h"
#import "ApplicationUtils.h"
@implementation SingleSelected_UIButton
@synthesize m_XingAndLabel_UIView;

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//初始化函数 背景大小为 111*46
- (id)initWithFrame:(CGRect)frame isNecessary:(BOOL)isNeedB withTitleStr:(NSString*)titleStr{
    //super method
    self = [super initWithFrame:frame];
    if (self) {
        float x,y,width,height;
        
        //左侧的“*”及文字
        x=8; y=0; width=frame.size.width-8-40; height=frame.size.height;
        float fontSizef=18.0;
        XingAndLabel_UIView* titleLabelView=[[XingAndLabel_UIView alloc] initWithFrame:CGRectMake(x, y, width, height) isNecessary:isNeedB 
                                                                          withTitleStr:titleStr with_FontOfSize:fontSizef];
        if (titleLabelView.titleLabel.frame.size.width>width) {
            [titleLabelView.titleLabel setFrame:CGRectMake(titleLabelView.titleLabel.frame.origin.x, titleLabelView.titleLabel.frame.origin.y, width, titleLabelView.titleLabel.frame.size.height)];
            [titleLabelView.titleLabel setFont:[UIFont systemFontOfSize:15.0]];
            [titleLabelView.titleLabel setNumberOfLines:0];
            [titleLabelView.titleLabel setLineBreakMode:NSLineBreakByWordWrapping];
            [titleLabelView.titleLabel setTextAlignment:NSTextAlignmentCenter];
            [titleLabelView.titleLabel setBackgroundColor:[UIColor clearColor]];
        }
        [titleLabelView setUserInteractionEnabled:NO];
        self.m_XingAndLabel_UIView=titleLabelView;
        [self addSubview:titleLabelView];
        
        //btn属性设置
        [self setBackgroundColor:[UIColor clearColor]];
        if (frame.size.width<=111) {
            [self setBackgroundImage:[ApplicationUtils getLocalImage:@"not_select"] forState:UIControlStateNormal];
            [self setBackgroundImage:[ApplicationUtils getLocalImage:@"select"] forState:UIControlStateSelected];
        }else {
            //其中Insets这个参数的格式是(top,left,bottom,right)，从上、左、下、右分别在图片上画了一道线，这样就给一个图片加了一个框。
            //只有在框里面的部分才会被拉伸，而框外面的部分则不会改变
            //(1)直接读取
            //UIImage* old_normal_Img=[[UIImage alloc] initWithCGImage:[ApplicationInfo getImageFromLocalFile:@"Images/qitaxinxi_anniu1"].CGImage scale:2.0 orientation:UIImageOrientationUp];
            //UIImage* normal_Img=[old_normal_Img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 40)];
            //[self setBackgroundImage:normal_Img forState:UIControlStateNormal];
            //[old_normal_Img release];
            
            //UIImage* old_select_Img=[[UIImage alloc] initWithCGImage:[ApplicationInfo getImageFromLocalFile:@"Images/qitaxinxi_anniu1_1"].CGImage scale:2.0 orientation:UIImageOrientationUp];
            //UIImage* select_Img=[old_select_Img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 40)];
            //[self setBackgroundImage:select_Img forState:UIControlStateSelected];
            //[old_select_Img release];
            //(2)缓存方式
            UIImage* old_normal_Img=[[UIImage alloc] initWithCGImage:[UIImage imageNamed:@"selector_lib_item_bg"].CGImage scale:2.0 orientation:UIImageOrientationUp];
            UIImage* normal_Img=[old_normal_Img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 40)];
            [self setBackgroundImage:normal_Img forState:UIControlStateNormal];
            
            UIImage* old_select_Img=[[UIImage alloc] initWithCGImage:[UIImage imageNamed:@"selector_lib_item_bg"].CGImage scale:2.0 orientation:UIImageOrientationUp];
            UIImage* select_Img=[old_select_Img resizableImageWithCapInsets:UIEdgeInsetsMake(0, 20, 0, 40)];
            [self setBackgroundImage:select_Img forState:UIControlStateSelected];
        }
    }
    return self;
}

- (void)dealloc {
    if (self.m_XingAndLabel_UIView) {
        self.m_XingAndLabel_UIView=nil;
    }
}

@end
